package com.aasa.siccosap.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.aasa.siccosap.bean.ObraNotaCredito;
import com.aasa.siccosap.bean.ParametrosConsultaObra;
import com.aasa.siccosap.util.Constantes;

public class ObraOtrasNotasCreditoDAO extends ConsultaObraBaseDAO{

	static Logger logger = Logger.getLogger("ObraOtrasNotasCreditoDAO.class");
	public ObraOtrasNotasCreditoDAO()	throws accesoDaoException {
		super();
	}
	
	public ObraOtrasNotasCreditoDAO(DataSource dataSource) {
		super(dataSource);
	}
	
	public List<ObraNotaCredito> consultarNotasCreditoSinObra(ParametrosConsultaObra parametros) throws Exception{
        ResultSet rs = null;
        this.av_message = null;
        this.storedProcedure = "begin CONSULTA_ACEDIM_PKG.P_CTACTE_OBRA_OTNC_SIN(?,?,?,?,?,?); end;";
        
        List<ObraNotaCredito> consultaNotasCreditoSinObra=new ArrayList<ObraNotaCredito>();
        
        try{
                this.conn = dataSource.getConnection();
                this.stmt = this.conn.prepareCall(storedProcedure);
                              
                configuraParametrosConsultaSinObra(parametros);
                
                this.stmt.execute();

                //Se obtiene la respuesta error/exito del stored
                this.av_message = (String)stmt.getObject(1);                	
                    if(this.av_message.trim().toUpperCase().endsWith(Constantes.NO_ERRORS)){
                        //Se obtiene el objeto del cursor
                        rs = (ResultSet)stmt.getObject(2);
                        while(rs.next()){
                        	ObraNotaCredito registroCtaCte = creaRegistroNotasCreditoSinObra(rs);
                        	consultaNotasCreditoSinObra.add(registroCtaCte);
                        }
                }else{
                    return null;
                }

        }catch (Exception e){
                e.printStackTrace();
                return null;
        }finally{
        		close(this.conn, rs, stmt);
        }
        return(consultaNotasCreditoSinObra);
        
    }

	private ObraNotaCredito creaRegistroNotasCreditoSinObra(ResultSet rs) throws SQLException {
		ObraNotaCredito bean = new ObraNotaCredito();
		
		bean.setStrPb_tipoDoc(rs.getString("DESC_TIP_DOC"));
		bean.setStrPb_serieManual(rs.getString("SERIE_MANUAL"));
		bean.setStrPb_numeroManual(rs.getString("NUMERO_MANUAL"));
		bean.setObjPb_fechaEmision(rs.getDate("FEC_EMI"));
		bean.setDblPb_toneladas(rs.getDouble("TONELADAS"));
		bean.setStrPb_moneda(rs.getString("DESC_MONEDA"));
		bean.setDblPb_importeDolares(rs.getDouble("IMPORTE_DOLARES"));
		bean.setDblPb_saldoDolares(rs.getDouble("SALDO_DOLARES"));
				
		return bean;
	}
}
